package com.aaa.controller;

import com.aaa.entity.Movie;
import com.aaa.entity.Mtype;
import com.aaa.entity.Product;
import com.aaa.entity.Ptype;
import com.aaa.service.MovieService;
import com.aaa.service.MtypeService;
import com.aaa.util.DefaultMsg;
import com.aaa.util.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/movie")
public class MovieController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private MtypeService mtypeService;
    @RequestMapping("/toList")
    public String toList(){
        return "movie/list";
    }
    @RequestMapping("/listData")
    @ResponseBody
    public TableData<Movie> listData(@RequestParam(defaultValue = "") String keyword, Integer page, Integer limit){
        TableData<Movie> page1 = movieService.page(keyword, page, limit);
        return page1;
    }
    @RequestMapping("/upload")
    @ResponseBody
    public DefaultMsg upload(MultipartFile file){
        String pictureName= UUID.randomUUID().toString();
        File saveFile = new File("F:/upload/"+pictureName);
        DefaultMsg defaultMsg = new DefaultMsg();
        try {
            file.transferTo(saveFile);
            defaultMsg.setTarget(pictureName);
        } catch (IOException e) {
            defaultMsg.setError("上传文件失败");
            e.printStackTrace();
        }
        return defaultMsg;
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public DefaultMsg saveOrUpdate(Movie movie){
        DefaultMsg defaultMsg = movieService.saveOrupdate(movie);
        return defaultMsg;
    }
    @RequestMapping("/toEdit")
    public String toEdit(){
        return "movie/edit";
    }
    /**
     *  根据商品编号删除商品信息
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public DefaultMsg delete(Long id){
        DefaultMsg defaultMsg = movieService.delete(id);
        return  defaultMsg;
    }
    @RequestMapping("/listAllMtypes")
    @ResponseBody
    public List<Mtype> listAllMtypes(){
        List<Mtype> mtypes = mtypeService.listAll();
        return mtypes;
    }
}
